﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseObj;
using System.Text;
using Business_Logic.Collections;
using Business_Logic.filter;

public partial class UserControls_ItemContent : System.Web.UI.UserControl
{
    #region 属性

    private string itemId;
    private string sqlOrder = "ORDER BY CreateDate DESC";
    private int titleWordCount = 15;
    private string infoUrl = "../info.aspx";

    /// <summary>
    /// 浏览页地址
    /// </summary>
    public string InfoUrl
    {
        get { return infoUrl; }
        set { infoUrl = value; }
    }

    /// <summary>
    /// 标题字数。
    /// </summary>
    public int TitleWordCount
    {
        get { return titleWordCount; }
        set { titleWordCount = value; }
    }

    /// <summary>
    /// 排序sql
    /// </summary>
    public string SqlOrder
    {
        get { return this.sqlOrder; }
        set { this.sqlOrder = value; }
    }

    /// <summary>
    /// 栏目id，可用逗号分割多个
    /// </summary>
    public string ItemId
    {
        get { return this.itemId; }
        set { this.itemId = value; }
    }
    private int pageIndex = 1;

    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageIndex
    {
        get { return pageIndex; }
        set { pageIndex = value; }
    }

    private int pageSize = 10;

    /// <summary>
    /// 分页大小
    /// </summary>
    public int PageSize
    {
        get { return pageSize; }
        set { pageSize = value; }
    }

    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.CheckField())
            {
                this.RenderThisHtml(new NewsCollection(this.CreateFilter()).ArrayNews);
            }
        }
    }


    #region 私有方法

    /// <summary>
    /// 输出栏目的html脚本
    /// </summary>
    private void RenderThisHtml(News[] ns)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<ul>");
        for(int i=0;i<ns.Length;i++)
        {
            News n = ns[i];
            sb.Append("<li><i></i><b><a href=\"");
            sb.Append(this.infoUrl);
            sb.Append("?Id=");
            sb.Append(n.NewsId);
            sb.Append("\" target=\"_blank\">");
            sb.Append(n.Title.Length <= this.titleWordCount ? n.Title : (n.Title.Substring(0, this.titleWordCount - 1) + "..."));
            sb.Append("</a></b><i>");
            sb.Append("[" + n.ReleasedDate + "]");
            sb.Append("</i></li>");
        }
        sb.Append("</ul>");
        this.litTxt.Text = sb.ToString();
        

    }

    /// <summary>
    /// 检测属性是否合乎规范
    /// </summary>
    /// <returns></returns>
    private bool CheckField()
    {
        if (this.itemId == null)
        {
            this.litTxt.Text = "请指定itemId";
            return false;
        }
        if (this.sqlOrder == "" || this.sqlOrder == null || this.sqlOrder == " ")
        {
            this.litTxt.Text = "指定排序属性不正确";
            return false;
        }
        return true;
    }

    /// <summary>
    /// 根据属性或者参数构造一个过滤器
    /// </summary>
    /// <returns></returns>
    private NewsFilter CreateFilter()
    {
        NewsFilter filter = new NewsFilter(this.pageIndex, this.pageSize, this.sqlOrder);
        filter.ItemId = this.itemId;
        return filter;
    }
    #endregion
}